axios.interceptors.request.use(config => {
    var token =  $.cookie('A_TOKEN')
    //如果已经登录了,每次都把token作为一个请求头传递过程
    if (token) {
        // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
        config.headers['Authorization'] = "bearer " + token
    }
    console.debug('config',config)

    var refreshToken = $.cookie("R_TOKEN");
    if (!refreshToken){
        window.location.href = "http://user.hrm.com:6002/login.html"
    }
    var expireTime = $.cookie("expireTime");
    var currentTime = new Date();
    var Stime = (currentTime - expireTime) / (60 * 1000);
    if (Stime >= -10 && Stime <= 5) {
        tokenRefresh(refreshToken);
    }

    return config
}, error => {
    Promise.reject(error)
})

function tokenRefresh(token){
    var refreshParams = {
        "refreshToken": token,
        "type": 0
    }
    this.$http.post("/auth/hrm/refreshToken",refreshParams).then(res=>{
        var ajaxResult = res.data;
        if(ajaxResult.success){
            var resultObj = ajaxResult.resultObj
            var EXPIRES_IN = resultObj.expires_in;
            var A_TOKEN = resultObj.access_token
            var R_TOKEN = resultObj.refresh_token

            // 获取当前时间毫秒值加上设置的过期时间的毫秒值
            var time = new Date().getTime() + EXPIRES_IN * 1000;
            $.cookie('A-TOKEN',A_TOKEN,{
                expires:new Date(time), //过期期间 如果是数字单位天，可以是日期
                path:'/', //根及其所有的子子孙孙都可以获取
                domain:'hrm.com', //域 www.hrm.com user.hrm.com xxx.hrm.com
                secure:false //是否支持https
            });
            $.cookie('R-TOKEN',R_TOKEN,{
                expires:7,
                path:'/',
                domain:'hrm.com',
                secure:false
            });
            $.cookie('expireTime',time,{
                expires:7,
                path:'/',
                domain:'hrm.com',
                secure:false
            });
        }else{
            alert("登录失败:"+ajaxResult.message);
        }
    })
}